#include "aepulseinfotab.h"
#include <QHeaderView>
#include <QHBoxLayout>
#include <QString>
#include "aepulsefeature.h"

AEPulseInfoTab::AEPulseInfoTab(QWidget *parent)
    : MapInfoTab{parent}
    , m_aePulseFeature{std::make_shared<AEPulseFeature>()}
    , m_pChartBox{new QGroupBox("AE脉冲图谱数据")}
    , m_pChartData{new QTextEdit}
    , m_pMainLayout{new QHBoxLayout(this)}
    , m_pLayout{new QHBoxLayout()}
{
    // 设置主布局
    m_pMainLayout->setContentsMargins(0, 0, 0, 0);
    m_pMainLayout->setSpacing(0);

    // 初始化Feature组件的UI并添加到布局
    m_aePulseFeature->initUI(this);
    addFeature(m_aePulseFeature);

    // 创建图表数据区域
    m_pChartData->setReadOnly(true);
    m_pChartData->setMinimumWidth(300);
    m_pChartData->setMinimumHeight(400);

    QVBoxLayout* pChartLayout = new QVBoxLayout(m_pChartBox);
    pChartLayout->addWidget(m_pChartData);
    pChartLayout->setContentsMargins(10, 10, 10, 10);

    m_pLayout->addWidget(m_pChartBox);
    m_pLayout->setContentsMargins(0, 0, 0, 0);

    m_pMainLayout->addLayout(m_pLayout);
    setLayout(m_pMainLayout);
}

AEPulseInfoTab::~AEPulseInfoTab()
{
    // UI元素会随父对象自动删除
}

/**
* @brief 设置检测数据类型编码
* @param code 数据类型编码
*/
void AEPulseInfoTab::setDataTypeCode(uint8_t code)
{
    // 数据会通过特性组件显示
    if (m_aePulseFeature) {
        m_aePulseFeature->setDataTypeCode(code);
    }
}

/**
* @brief 设置图谱数据长度
* @param length 数据长度
*/
void AEPulseInfoTab::setDataLength(int32_t length)
{
    // 数据会通过特性组件显示
    if (m_aePulseFeature) {
        m_aePulseFeature->setDataLength(length);
    }
}

/**
* @brief 设置图谱生成时间
* @param time 生成时间
*/
void AEPulseInfoTab::setCreateTime(const QString& time)
{
    // 数据会通过特性组件显示
    if (m_aePulseFeature) {
        m_aePulseFeature->setCreateTime(time);
    }
}

/**
* @brief 设置图谱性质
* @param property 图谱性质
*/
void AEPulseInfoTab::setChartProperty(const DataSpecificationGW::SpectrumCharacter& property)
{
    // 数据会通过特性组件显示
    if (m_aePulseFeature) {
        m_aePulseFeature->setChartProperty(property);
    }
}

/**
* @brief 设置被测设备名称
* @param name 设备名称
*/
void AEPulseInfoTab::setDeviceName(const QString& name)
{
    // 数据会通过特性组件显示
    if (m_aePulseFeature) {
        m_aePulseFeature->setDeviceName(name);
    }
}

/**
* @brief 设置被测设备编码
* @param code 设备编码
*/
void AEPulseInfoTab::setDeviceCode(const QString& code)
{
    // 数据会通过特性组件显示
    if (m_aePulseFeature) {
        m_aePulseFeature->setDeviceCode(code);
    }
}

/**
* @brief 设置测点名称
* @param name 测点名称
*/
void AEPulseInfoTab::setMeasurementName(const QString& name)
{
    // 数据会通过特性组件显示
    if (m_aePulseFeature) {
        m_aePulseFeature->setMeasurementName(name);
    }
}

/**
* @brief 设置测点编码
* @param code 测点编码
*/
void AEPulseInfoTab::setMeasurementCode(const QString& code)
{
    // 数据会通过特性组件显示
    if (m_aePulseFeature) {
        m_aePulseFeature->setMeasurementCode(code);
    }
}

/**
* @brief 设置检测通道标志
* @param flag 通道标志
*/
void AEPulseInfoTab::setChannelFlag(int64_t flag)
{
    // 数据会通过特性组件显示
    if (m_aePulseFeature) {
        m_aePulseFeature->setChannelFlag(flag);
    }
}

/**
* @brief 设置存储数据类型
* @param type 数据类型
*/
void AEPulseInfoTab::setDataType(uint8_t type)
{
    // 数据会通过特性组件显示
    if (m_aePulseFeature) {
        m_aePulseFeature->setDataType(type);
    }
}

/**
* @brief 设置幅值单位
* @param unit 幅值单位
*/
void AEPulseInfoTab::setAmplitudeUnit(uint8_t unit)
{
    if (m_aePulseFeature) {
        m_aePulseFeature->setAmplitudeUnit(unit);
    }
}

/**
* @brief 设置幅值下限
* @param lowerLimit 下限值
*/
void AEPulseInfoTab::setAmplitudeLowerLimit(float lowerLimit)
{
    if (m_aePulseFeature) {
        m_aePulseFeature->setAmplitudeLowerLimit(lowerLimit);
    }
}

/**
* @brief 设置幅值上限
* @param upperLimit 上限值
*/
void AEPulseInfoTab::setAmplitudeUpperLimit(float upperLimit)
{
    if (m_aePulseFeature) {
        m_aePulseFeature->setAmplitudeUpperLimit(upperLimit);
    }
}

/**
* @brief 设置超声波传感器类型
* @param type 传感器类型
*/
void AEPulseInfoTab::setUltrasonicSensorType(float type)
{
    if (m_aePulseFeature) {
        m_aePulseFeature->setUltrasonicSensorType(type);
    }
}

/**
* @brief 设置脉冲间隔时间单位
* @param unit 时间单位
*/
void AEPulseInfoTab::setPulseIntervalTimeUnit(uint8_t unit)
{
    if (m_aePulseFeature) {
        m_aePulseFeature->setPulseIntervalTimeUnit(unit);
    }
}

/**
* @brief 设置数据点数
* @param count 数据点数
*/
void AEPulseInfoTab::setDataPointCount(int32_t count)
{
    if (m_aePulseFeature) {
        m_aePulseFeature->setDataPointCount(count);
    }
}

/**
* @brief 设置放电类型概率
* @param probability 概率值
*/
void AEPulseInfoTab::setDischargeTypeProbability(const QString& probability)
{
    if (m_aePulseFeature) {
        m_aePulseFeature->setDischargeTypeProbability(probability);
    }
}

/**
* @brief 设置预留数据
* @param byte 预留数据
*/
void AEPulseInfoTab::setReservedData(const QByteArray& byte)
{
    // 数据会通过特性组件显示
    if (m_aePulseFeature) {
        m_aePulseFeature->setReservedData(byte);
    }
}

/**
* @brief 设置AE脉冲图谱数据
* @param data 脉冲图谱数据
*/
void AEPulseInfoTab::setAEPulseChartData(const QByteArray& data)
{
    m_pChartData->setText(data.toHex());
}

QTableWidgetItem *AEPulseInfoTab::createTableWidgetItem(const QString &text)
{
    QTableWidgetItem *item = new QTableWidgetItem();
    QFont font;
    font.setBold(true);
    item->setFont(font);
    item->setTextAlignment(Qt::AlignCenter);
    item->setText(text);
    return item;
}
